﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using EthernetTools.Utils;

namespace EthernetTools.DataBindinds
{
    class ListStringToBytes : ConrolBinding
    {
        List<object> DataList = new List<object>();

        private System.EventHandler UpdateUICall;

        public object SelectedItem
        {
            get
            {
                if (stringContainer == null)
                {
                    if (DataList != null)
                    {
                        if (DataList.Count > 0)
                        {
                            return DataList[0];
                        }
                    }
                    return null;
                }

                if (!(stringContainer is ComboBox))
                {
                    if (DataList != null)
                    {
                        if (DataList.Count > 0)
                        {
                            return DataList[0];
                        }
                    }
                    return null;
                }

                if (((ComboBox)stringContainer).SelectedItem == null)
                {
                    if (DataList != null)
                    {
                        if (DataList.Count > 0)
                        {
                            return DataList[0];
                        }
                    }
                    return null;
                }
                return ((ComboBox)stringContainer).SelectedItem;
            }
        }

        /// <summary>
        /// Gets the Raw byte[] data of binding (if present), null otherwise
        /// </summary>
        public override byte[] RawData
        {
            get
            {
                bool getFromList = false; 

                if (stringContainer == null)
                {
                    getFromList = true;
                }

                if (!(stringContainer is ComboBox))
                {
                    getFromList = true;
                }

                if (((ComboBox)stringContainer).SelectedItem == null)
                {
                    getFromList = true;
                }

                if (!getFromList)
                {
                    Object selected = ((ComboBox)stringContainer).SelectedItem;

                    if (selected is EthernetTools.Protocols.PPPoE.PppoeProtocolPacket.PPPoEType)
                    {
                        return ((EthernetTools.Protocols.PPPoE.PppoeProtocolPacket.PPPoEType)selected).ToByteArray();
                    }
                }
                if (DataList != null)
                {
                    if (DataList.Count > 0)
                    {
                        return ByteUtils.ParseBytes(DataList[0].ToString());
                    }
                }
                return null;
            }
        }

        /// <summary>
        /// Was data in control successfully parsed
        /// </summary>
        public override bool IsCorrect
        {
            get
            {
                if (stringContainer == null)
                {
                    return DataList == null;
                }

                if (!(stringContainer is ComboBox))
                {
                    return DataList == null;
                }

                if (((ComboBox)stringContainer).SelectedItem == null)
                {
                    return DataList == null;
                }

                return DataList == null;
            }
            protected set
            {
                if (stringContainer == null && DataList == null)
                {
                    return;
                }

                if (!(stringContainer is ComboBox) && DataList == null)
                {
                    return;
                }

                if (((ComboBox)stringContainer).SelectedItem == null && DataList == null)
                {
                    return;
                }

                isCorrect = value;
            }
        }

        public void Bind(System.Windows.Forms.ComboBox listContainer,
            System.EventHandler UpdateUIFunction,
            List<object> dataList
            )
        {
            this.UpdateUICall = UpdateUIFunction;

            this.stringContainer = listContainer;

            this.DataList = dataList;

            if (this.stringContainer != null)
            {
                ((ComboBox)this.stringContainer).Items.Clear();

                ((ComboBox)this.stringContainer).Items.AddRange(DataList.ToArray());

                ((ComboBox)this.stringContainer).SelectionChangeCommitted += new EventHandler(currentTypeContainer_SelectionChangeCommitted);
            }

            base.Bind();
        }

        public override void UnBind()
        {
            base.UnBind();

            this.UpdateUICall = null;
            
            if (this.stringContainer != null)
            {
                ((ComboBox)this.stringContainer).SelectionChangeCommitted -= currentTypeContainer_SelectionChangeCommitted;
            }

        }

        public void Select(int index)
        {
            if ( index < 0)
            {
                return;
            }

            if (stringContainer != null)
            {
                if (((ComboBox)stringContainer).Items.Count > index)
                {
                    ((ComboBox)stringContainer).SelectedIndex = index;
                }
            }
        }

        void currentTypeContainer_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (UpdateUICall != null)
                UpdateUICall(sender, e);
        }
    }
}
